//
//  mluLawyerCaseSituationsList.m
//  mluLawyer
//
//  Created by Paulo Ferreira on 12/1/09.
//  Copyright 2009 MobileLifeUtils.com. All rights reserved.
//

#import "mluLawyerCaseSituationsList.h"


@implementation mluLawyerCaseSituationsList

@synthesize mocCaseSituations, maCaseSituations, frCaseSituations, edCaseSituations;

/*
- (id)initWithStyle:(UITableViewStyle)style {
    // Override initWithStyle: if you create the controller programmatically and want to perform customization that is not appropriate for viewDidLoad.
    if (self = [super initWithStyle:style]) {
    }
    return self;
}
*/

- (void)viewDidLoad {
	self.title = NSLocalizedString(@"titCaseSituations", nil);
	
	UIBarButtonItem * bbiNew = [[UIBarButtonItem alloc] 
								initWithTitle:NSLocalizedString(@"btNew", nil)
								style:UIBarButtonItemStyleBordered
								target:self 
								action:@selector(newCaseSituation)
								];
	
	self.toolbarItems = [[NSArray alloc] initWithObjects:bbiNew, nil];
	self.navigationController.toolbarHidden = NO;
	
	frCaseSituations = [[NSFetchRequest alloc] init];
	
	NSSortDescriptor *sdCaseSituations = [[NSSortDescriptor alloc] initWithKey:@"name" ascending:YES];
	
	[frCaseSituations setSortDescriptors:[[NSArray alloc] initWithObjects:sdCaseSituations, nil]];
	
	edCaseSituations = [NSEntityDescription entityForName:@"case_situations" inManagedObjectContext:mocCaseSituations];
	
	[frCaseSituations setEntity:edCaseSituations];
	
	NSError *errFetchRequest;
	maCaseSituations = [[mocCaseSituations executeFetchRequest:frCaseSituations error:&errFetchRequest] mutableCopy];
	
	[super viewDidLoad];
}

- (void)viewWillAppear:(BOOL)animated {
	NSError *errFetchRequest;
	maCaseSituations = [[mocCaseSituations executeFetchRequest:frCaseSituations error:&errFetchRequest] mutableCopy];
	[self.tableView reloadData];
	[super viewWillAppear:animated];
}


- (void)viewDidAppear:(BOOL)animated {
    self.navigationController.toolbarHidden = NO;
	[super viewDidAppear:animated];
}

/*
- (void)viewWillDisappear:(BOOL)animated {
	[super viewWillDisappear:animated];
}
*/
/*
- (void)viewDidDisappear:(BOOL)animated {
	[super viewDidDisappear:animated];
}
*/

/*
// Override to allow orientations other than the default portrait orientation.
- (BOOL)shouldAutorotateToInterfaceOrientation:(UIInterfaceOrientation)interfaceOrientation {
    // Return YES for supported orientations
    return (interfaceOrientation == UIInterfaceOrientationPortrait);
}
*/

- (void)didReceiveMemoryWarning {
	// Releases the view if it doesn't have a superview.
    [super didReceiveMemoryWarning];
	
	// Release any cached data, images, etc that aren't in use.
}

- (void)viewDidUnload {
	// Release any retained subviews of the main view.
	// e.g. self.myOutlet = nil;
}

#pragma mark -
#pragma mark UITableViewDelegate methods

- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView {
	return 1;
}


// Customize the number of rows in the table view.
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section {
    return [maCaseSituations count];
}


// Customize the appearance of table view cells.
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
    
    static NSString *CellIdentifier = @"Cell";
    
    UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier];
    if (cell == nil) {
        cell = [[[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:CellIdentifier] autorelease];
    } 
	
	for (UIView *vwExisting in cell.contentView.subviews) {
		[vwExisting removeFromSuperview];
	}
    
    NSEntityDescription *edCurrentRecord = [maCaseSituations objectAtIndex:indexPath.row];
	
	UILabel *lblCell = [[UILabel alloc] initWithFrame:CGRectMake(5.0, 5.0, 280, 20.0)];
	[lblCell setText:edCurrentRecord.name];
	
	[cell.contentView addSubview:lblCell];
	
	[cell setSelectionStyle:UITableViewCellSelectionStyleNone];
	
    return cell;
}


- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath {
    mluLawyerCaseSituationsEdit *vcCaseSituationsEdit = [[mluLawyerCaseSituationsEdit alloc] initWithStyle:UITableViewStyleGrouped];
	vcCaseSituationsEdit.mocCaseSituationsEdit = mocCaseSituations;
	vcCaseSituationsEdit.edCaseSituation = [maCaseSituations objectAtIndex:indexPath.row];
	[self.navigationController pushViewController:vcCaseSituationsEdit animated:YES];
}


/*
// Override to support conditional editing of the table view.
- (BOOL)tableView:(UITableView *)tableView canEditRowAtIndexPath:(NSIndexPath *)indexPath {
    // Return NO if you do not want the specified item to be editable.
    return YES;
}
*/



// Override to support editing the table view.
- (void)tableView:(UITableView *)tableView commitEditingStyle:(UITableViewCellEditingStyle)editingStyle forRowAtIndexPath:(NSIndexPath *)indexPath {
    
    if (editingStyle == UITableViewCellEditingStyleDelete) {
        NSEntityDescription *edCaseSituation = [maCaseSituations objectAtIndex:indexPath.row];
		[maCaseSituations removeObject:edCaseSituation];
		[mocCaseSituations deleteObject:(NSManagedObject *)edCaseSituation];
		NSError *errDelete;
		if (![mocCaseSituations save:&errDelete ]) {
			NSLog(@"%@", [errDelete localizedDescription]);
		}
        [tableView deleteRowsAtIndexPaths:[NSArray arrayWithObject:indexPath] withRowAnimation:YES];
    }   
}

/*
// Override to support rearranging the table view.
- (void)tableView:(UITableView *)tableView moveRowAtIndexPath:(NSIndexPath *)fromIndexPath toIndexPath:(NSIndexPath *)toIndexPath {
}
*/


/*
// Override to support conditional rearranging of the table view.
- (BOOL)tableView:(UITableView *)tableView canMoveRowAtIndexPath:(NSIndexPath *)indexPath {
    // Return NO if you do not want the item to be re-orderable.
    return YES;
}
*/

#pragma mark -
#pragma mark Custom Methods

- (void)newCaseSituation {
	mluLawyerCaseSituationsEdit *vcCaseSituationsEdit = [[mluLawyerCaseSituationsEdit alloc] initWithStyle:UITableViewStyleGrouped];
	vcCaseSituationsEdit.mocCaseSituationsEdit = mocCaseSituations;
	[self.navigationController pushViewController:vcCaseSituationsEdit animated:YES];
}


- (void)dealloc {
    [super dealloc];
}


@end

